<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace plugins\user_other\model;

use think\Db;
use think\Model;

class PluginUserOtherModel extends Model
{



    //自定义方法
    public static function get_all_users($where = array(), $num = 20)
    {
    }

    /**
     * @msg: 用户角色对应
     */
    public static function get_role($list)
    {
        $role =  Db::name('role')->column('id,name');
        $role_user = Db::name('role_user')->column('user_id,role_id');
        $role_user[1] = 1;
        foreach ($list as $key => $value) {
            if (!empty($role_user[$value['id']])) {
                $value['user_type'] = $role[$role_user[$value['id']]];
            } else {
                $value['user_type'] = '普通用户';
            }
            $list[$key] = $value;
        }
        return $list;
    }

    public static function get_rank()
    {
		$ranklist[0] = "普通会员";
		$ranklist +=  Db::name('user_rank')->column('id,name');
		
        return $ranklist;
    }

    /**
     * @msg: 用户状态列表
     */
    public static function user_status()
    {
        return [0 => ['id' => 0, 'name' => '已禁用'], 1 => ['id' => 1, 'name' => '正常'], 2 => ['id' => 2, 'name' => '未验证']];
    }

    public static function user_data()
    {
        return ['user_login' => '1', 'rank_id' => '', 'mobile' => '1', 'user_pass' => '', 'user_nickname' => '', 'score' => '', 'coin' => '', 'balance' => '', 'user_email' => ''];
    }

    /**
     * @msg: 新建用户必须表单
     */
    public static function need_in()
    {
        return ['user_login' => '登录名', 'mobile' => '手机号', 'user_nickname' => '昵称'];
    }

    /**
     * @msg: 更新用户列表
     */
    public static function update_user($data = [], $user = [])
    {
        //处理密码
        if (!empty($data['user_pass'])) {
            $data['user_pass'] = cmf_password($data['user_pass']);
        }
        foreach ($user as $key => $value) {
            if (!empty($data[$key])) {
                $user[$key] = $data[$key];
            }
        }
        if (!empty($data['id'])) {
            $re = Db::name('user')->update($user);
        } else {
            $user['create_time'] = time();
            $re = Db::name('user')->insert($user);
        }
        return $re;
    }

    /**
     * @msg: 唯一判断
     */
    public static function user_only($data)
    {
        $user = Db::name('user')->where('user_login', $data['user_login'])->find();
        if ($user) {
            if ($user['id'] != $data['id']) {
                return '登录名已经存在';
            }
        }
        $user = Db::name('user')->where('mobile', $data['mobile'])->find();
        if ($user) {
            if ($user['id'] != $data['id']) {
                return '手机号已经存在';
            }
        }
        $user = Db::name('user')->where('user_nickname', $data['user_nickname'])->find();
        if ($user) {
            if ($user['id'] != $data['id']) {
                return '用户昵称已存在';
            }
        }
        return false;
    }
}
